﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SJTU.SE.RoughSet.Lib;
using SJTU.SE.RoughSet.Lib.IO;
using SJTU.SE.RoughSet.Lib.Data;
using SJTU.SE.RoughSet.Lib.Reduct;

namespace SJTU.SE.RoughSet
{
    public partial class Main : Form
    {
        #region property
        private bool mFileOpened = false;
        private string mDataFilePath;
        private DecisionTable dt;
        private Reductor rd;
        List<int> core;


        #endregion


        public Main()
        {
            InitializeComponent();

            //move this to init
            Icon icon = new Icon(".\\resource\\data.ico");
            this.listView1.LargeImageList = new ImageList();
            this.listView1.LargeImageList.Images.Add(icon);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Rough Set Toolset.");
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.mFileOpened = true;
                mDataFilePath = this.openFileDialog1.FileName;
                this.listView1.Items.Clear();
                this.listView1.Items.Add(this.openFileDialog1.SafeFileName,0);

                IDecisionTableFileInitializer fileInit = new DecisionTableFileInitializer(this.mDataFilePath);
                dt = new DecisionTable(fileInit);
                rd = new Reductor(dt);
            }
        }

        private void coreToolStripMenuItem1_Click(object sender, EventArgs e)
        {           
            core = rd.GetSkowronCore();
            StringBuilder coreMessage = new StringBuilder();
            coreMessage.Append("Attributes in Core are: ");
            foreach(var i in core)
            {
                coreMessage.Append("\r\n");
                coreMessage.Append(i);
            }

            MessageBox.Show(coreMessage.ToString());
        }

        private void ruleInductionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void reductionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var reductions = rd.GetAllReductionsByTraversal();
            StringBuilder reductionMessage = new StringBuilder();
            reductionMessage.Append("All Reductions:");
            foreach (var reduction in reductions)
            {
                reductionMessage.Append("\r\n");
                foreach (var i in reduction)
                {
                    reductionMessage.Append(i);
                    reductionMessage.Append(", ");
                }
            }

            MessageBox.Show(reductionMessage.ToString());
        }

        private void fissionReductionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            rd.GetSkowronCore();
            rd.GetDiscernibilityMatrix();
            var reductions = rd.GetAllReductionsByTraversal();
            StringBuilder reductionMessage = new StringBuilder();
            reductionMessage.Append("All Reductions:");
            foreach (var reduction in reductions)
            {
                reductionMessage.Append("\r\n");
                foreach (var i in reduction)
                {
                    reductionMessage.Append(i);
                    reductionMessage.Append(", ");
                }
            }

            MessageBox.Show(reductionMessage.ToString());
        }        
    }
}
